## This file creates figures A1 and A5


library(tidyverse)


dta <- haven::read_dta("NonIdentifyingReplicationDataLegAudit.dta")


dta <- dta %>%
  filter(state != "NE")


#### Figure A1


group_means <- dta %>% 
  filter(mailerdaemon != 1 | is.na(mailerdaemon)) %>%
  filter(!is.na(FemaleSender) & !is.na(legFemale) & !is.na(treatmentcoded)) %>% 
  rename(`Gender of Emailer` = FemaleSender) %>% 
  mutate(treatmentcoded = case_when(
    treatmentcoded == 0 ~ "Control",
    treatmentcoded == 1 ~ "Experience",
    treatmentcoded == 2 ~ "Ideol. extreme",
    treatmentcoded == 3 ~ "Ideol. moderate"
  )) %>%
  mutate(democrat = ifelse(democrat == 1, "Democrat Leg.", "Republican Leg.")) %>% 
  mutate(`Gender of Emailer` = ifelse(`Gender of Emailer` == 1, "Woman", "Man")) %>% 
  mutate(legFemale = ifelse(legFemale == 1, "Woman Leg.", "Man Leg.")) %>% 
  group_by(legFemale, democrat, `Gender of Emailer`) %>% 
  summarize(mean_response = mean(HelpfulResponse))



dta %>% 
  filter(mailerdaemon != 1 | is.na(mailerdaemon)) %>%
  filter(!is.na(FemaleSender) & !is.na(legFemale) & !is.na(treatmentcoded)) %>% 
  rename(`Gender of Emailer` = FemaleSender) %>% 
  mutate(treatmentcoded = case_when(
    treatmentcoded == 0 ~ "Control",
    treatmentcoded == 1 ~ "Experience",
    treatmentcoded == 2 ~ "Ideol. extreme",
    treatmentcoded == 3 ~ "Ideol. moderate"
  )) %>%
  mutate(democrat = ifelse(democrat == 1, "Democrat Leg.", "Republican Leg.")) %>% 
  mutate(`Gender of Emailer` = ifelse(`Gender of Emailer` == 1, "Woman", "Man")) %>% 
  mutate(legFemale = ifelse(legFemale == 1, "Woman Leg.", "Man Leg.")) %>% 
  group_by(treatmentcoded, `Gender of Emailer`, legFemale, democrat) %>% 
  summarize(mean_response = mean(HelpfulResponse, na.rm=T), sd_response = sd(HelpfulResponse, na.rm=T)) %>% 
  ungroup %>% 
  mutate(conf.low = mean_response - (1.96*sd_response), conf.high = mean_response + (1.96*sd_response)) %>% 
  mutate(mean_response = round(mean_response, 2)) %>% 
  mutate(sd_response = round(sd_response, 2) %>% paste("(", ., ")", sep="")) %>% 
  ggplot(aes(x = treatmentcoded, y = mean_response, shape = `Gender of Emailer`, group = `Gender of Emailer`, fill = `Gender of Emailer`)) +
  geom_hline(
    data = group_means %>% 
      rename(`Avg. Response to:` = `Gender of Emailer`) %>% 
      mutate(`Avg. Response to:` = paste(`Avg. Response to:`, "sender")),
    aes(yintercept = mean_response, linetype = `Avg. Response to:`),
    alpha=.5
  ) +
  geom_col(position = position_dodge(), width = .85, color = "black", alpha=.7) +
  geom_label(aes(y = mean_response+.015, label = mean_response), position = position_dodge(width = 0.8), size = 3, fill = "white") +
  geom_text(aes(y = mean_response-.01, label = sd_response), position = position_dodge(width = 0.8), size=2.5) +
  scale_fill_manual(values = c("grey", "white")) +
  facet_grid(legFemale ~ democrat) +
  theme_bw() +
  theme(
    panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
    axis.text.x = element_text(angle = 45, vjust = 1.1, hjust = 1.1),
    legend.position = "bottom"
  ) +
  xlab("Treatment Condition") +
  ylab("Mean Response") 


ggsave(file = "figureA1.pdf", units = "in", width = 8, height = 6)




#### Figure A5

#devtools::install_github("wmurphyrd/fiftystater")
library(mapproj)
library(fiftystater)



dta %>% 
  ungroup %>% 
  group_by(state, waveid) %>% 
  summarize(n = n(), response = sum(HelpfulResponse)) %>% 
  mutate(rr = response/n) %>% 
  ungroup %>% 
  group_by(state) %>% 
  summarize(rr = mean(rr)) %>% 
  left_join(data.frame(state = state.abb, region = str_to_lower(state.name))) %>% 
  rename(`Resp. Rate` = rr) %>% 
  bind_rows(tibble(state = "NE", region = 'nebraska', `Resp. Rate` = NA)) %>%
  filter(state != "") %>% 
  ggplot(aes(map_id = region)) + 
  geom_map(aes(fill = `Resp. Rate`), color = "black", map = fifty_states) + 
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  coord_map() +
  scale_fill_gradient(low = "white", high = "darkred", na.value = "#666666") +
  theme_void()


ggsave(file = "figureA5.pdf",
       units = "in", width=10, height=5)
